﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using bdnt_pets_system_Entity;
using System.Data;

namespace bdnt_pets_system_Bo.SystemManager
{
    public class RoleManager
    {
        //AccessHelperSQL accessHelper = AccessHelperSQL.GetInstance();
        AccessHelper accessHelper = AccessHelper.GetInstance();
        List<Tbl_Role> ConvertTableToObjs(DataTable dt)
        {
            List<Tbl_Role> returnlist = new List<Tbl_Role>();
            foreach (DataRow dr in dt.Rows)
            {
                returnlist.Add(new Tbl_Role
                {
                    Id = Convert.ToInt32(dr["Id"]),
                    RoleName = dr["RoleName"] == DBNull.Value ? "" : dr["RoleName"].ToString(),
                    RoleState = dr["RoleState"] == DBNull.Value ? 0 : Int32.Parse(dr["RoleState"].ToString()),
                    Remark = dr["Remark"] == DBNull.Value ? "" : dr["Remark"].ToString(),
                    RoleStateStr = dr["RoleStateStr"] == DBNull.Value ? "" : dr["RoleStateStr"].ToString()
                });

            }
            return returnlist;
        }

        public List<Tbl_Role> GetObjListByCondition(string rolName)
        {
            var sql = string.Format("select r.*,(case when RoleState=1 then '正常' else '关闭' end) RoleStateStr from Tbl_Role r");
            if (!string.IsNullOrEmpty(rolName))
                sql = string.Format("select r.*,(case when RoleState=1 then '正常' else '关闭' end) RoleStateStr from Tbl_Role r where roleName like '%{0}%'", rolName);
            var dt = accessHelper.ExcuteDataTable(sql);
            return ConvertTableToObjs(dt);
        }

        public Tbl_Role GetObjById(int roleId)
        {
            Tbl_Role cur = new Tbl_Role();
            var sql = string.Format(@"select rl.*, 
             RTRIM(replace(replace(replace(replace(replace(replace((
            select SYS_XMLAGG(SYS_XMLGEN(rm.menuId)) from Tbl_RoleMenu rm                  
            where rm.roleId=rl.Id                
            ) ,'</MENUID>',','),'<MENUID>',''),'<?xml version=""1.0""?>',''),'<ROWSET>',''),'</ROWSET>','') ,chr(10),''),',')
            MeunIds from Tbl_Role rl where Id={0}", roleId);
            var dt = accessHelper.ExcuteDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                cur = new Tbl_Role
                     {
                         Id = Convert.ToInt32(dr["Id"]),
                         RoleName = dr["RoleName"] == DBNull.Value ? "" : dr["RoleName"].ToString(),
                         RoleState = dr["RoleState"] == DBNull.Value ? 0 : Int32.Parse(dr["RoleState"].ToString()),
                         Remark = dr["Remark"] == DBNull.Value ? "" : dr["Remark"].ToString(),
                         MeunIds = dr["MeunIds"] == DBNull.Value ? "" : dr["MeunIds"].ToString()
                     };
            }
            return cur;
        }

        public bool SaveObj(Tbl_Role cuObj, List<int> menuIds)
        {
            string sql = "";
            //新增
            if (cuObj.Id == 0)
            {
                sql = string.Format(@"INSERT INTO Tbl_Role
               (RoleName,RoleState,Remark)
                VALUES('{0}',{1},'{2}')", cuObj.RoleName, cuObj.RoleState, cuObj.Remark);
                string sql1 = "select role_seq.currval from dual";
                var re = accessHelper.ExecuteScalar(sql, sql1);
                if (re == null)
                    return false;

                if (menuIds == null || menuIds.Count < 1)
                    return true;

                sql = string.Format(@"INSERT INTO Tbl_RoleMenu
                       (MenuId,RoleId,Sequence)
                        select Id,{0},menuSequence from Tbl_Menu where id in ({1})", Int32.Parse(re.ToString()), string.Join(",", menuIds));
                return accessHelper.ExcultData(sql);

            }
            //修改
            else
            {
                sql = string.Format(@"UPDATE Tbl_Role
               SET RoleName ='{0}'
                  ,RoleState = {1}
                  ,Remark = '{2}' 
             WHERE Id={3}", cuObj.RoleName, cuObj.RoleState, cuObj.Remark, cuObj.Id);

                var re = accessHelper.ExcultData(sql);
                if (re == true)
                {
                    sql = string.Format(@"call UpdateRole('{0}',{1})", string.Join(",", menuIds), cuObj.Id);
                    return accessHelper.ExcultProc(sql);
                }

                return false;
            }
        }

        public bool DeleteCuObj(Int32 objId)
        {
            string sql = string.Format(@"call DeleteRole({0})", objId);
            return accessHelper.ExcultProc(sql);
        }
    }
}
